/**
 * 
 */
package kr.co.insoft.auth.model.security;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
public class User implements UserDetails {

	/**
	 * 
	 */
	private static final long serialVersionUID = -191096453687943409L;

	Long id;
	String username;
	String password;
	String nickname;
	HashSet<Role> roles;
	Boolean accountNonExpired = false;
	Boolean accountNonLocked = false;
	Boolean credentialsNonExpired = false;
	Boolean enabled = false;

	public User() {
	}

	public User(final Long id, final String username, final String password) {
		this.id = id;
		this.username = username;
		this.password = password;
		this.roles = new HashSet<Role>();
	}

	public User(final Long id, final String username, final String password,
			final String nickname) {
		this(id, username, password);
		this.nickname = nickname;
	}

	public User(final Long id, final String username, final String password,
			final String nickname, final HashSet<Role> authorities) {
		this(id, username, password, nickname);
		this.roles = authorities;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.security.core.userdetails.UserDetails#getAuthorities
	 * ()
	 */
	public Collection<? extends GrantedAuthority> getAuthorities() {
		HashSet<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
		for (Role role : getRoles()) {
			authorities.add(new SimpleGrantedAuthority(role.getRole()));
		}
		return authorities;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.security.core.userdetails.UserDetails#getPassword()
	 * 
	 * Password never return!!
	 */
	public String getPassword() {
		return this.password;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.security.core.userdetails.UserDetails#getUsername()
	 */
	public String getUsername() {
		return this.username;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.security.core.userdetails.UserDetails#isAccountNonExpired
	 * ()
	 */
	public boolean isAccountNonExpired() {
		return this.accountNonExpired;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.security.core.userdetails.UserDetails#isAccountNonLocked
	 * ()
	 */
	public boolean isAccountNonLocked() {
		return this.accountNonLocked;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetails#
	 * isCredentialsNonExpired()
	 */
	public boolean isCredentialsNonExpired() {
		return this.credentialsNonExpired;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.security.core.userdetails.UserDetails#isEnabled()
	 */
	public boolean isEnabled() {
		return this.enabled;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the nickname
	 */
	public String getNickname() {
		return nickname;
	}

	/**
	 * @param nickname
	 *            the nickname to set
	 */
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	/**
	 * @return the roles
	 */
	public HashSet<Role> getRoles() {
		return this.roles;
	}

	public String getCompanyId() {
		String companyid = null;
		breakFor:for(GrantedAuthority auth : getAuthorities()) {
			if (auth.getAuthority().startsWith("ORG")) {
				companyid = auth.getAuthority().replace("ORG_", "");
				break breakFor;
			}
		}
		return companyid;
	}
	
	public HashMap<String, String> getQueryAuthParam() {
		HashMap<String, String> param = new HashMap<String, String>();
		param.put("userid", Long.toString(this.id));
		param.put("companyid", getCompanyId());
		return param;
	}
	
	public void addRole(Role role) {
		this.roles.add(role);
	}

	public void addRoles(HashSet<Role> roles) {
		this.roles = roles;
	}

	/**
	 * @param roles
	 *            the roles to set
	 */
	public void setRoles(HashSet<Role> roles) {
		this.roles = roles;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @param accountNonExpired
	 *            the accountNonExpired to set
	 */
	public void setAccountNonExpired(Boolean accountNonExpired) {
		this.accountNonExpired = accountNonExpired;
	}

	/**
	 * @param accountNonLocked
	 *            the accountNonLocked to set
	 */
	public void setAccountNonLocked(Boolean accountNonLocked) {
		this.accountNonLocked = accountNonLocked;
	}

	/**
	 * @param credentialsNonExpired
	 *            the credentialsNonExpired to set
	 */
	public void setCredentialsNonExpired(Boolean credentialsNonExpired) {
		this.credentialsNonExpired = credentialsNonExpired;
	}

	/**
	 * @param enabled
	 *            the enabled to set
	 */
	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

}
